package com.yiren.backstage.judge.thread;

import android.content.Context;

import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.currency.enums.TaskStateEnum;
import com.yiren.backstage.judge.api.IExamStateCallBack;
import com.yiren.backstage.judge.handler.AutoJudgeHandler;
import com.yiren.backstage.judge.services.standard.IAutoPlayService;
import com.yiren.backstage.judge.services.standard.impl.AutoPlayServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Time:2022/3/10 16:29
 * Author:xiaozan
 * Description:自动播报线程
 */
public class AutoPlayThread extends Thread {

    private Context mContext;
    private AutoJudgeHandler autoJudgeHandler;
    private LineInfoEntity mLineInfo;

    private List<LinePointBean> mExecutedPoint;  //已播报项目集合
    private GpsInfoBean gpsInfo;
    private IAutoPlayService autoPlayService;
    private IExamStateCallBack iExamStateCallBack;

    public AutoPlayThread(Context context, AutoJudgeHandler autoJudgeHandler, LineInfoEntity lineInfo, IExamStateCallBack iExamStateCallBack) {
        this.mContext = context;
        this.autoJudgeHandler = autoJudgeHandler;
        this.mLineInfo = lineInfo;
        this.iExamStateCallBack = iExamStateCallBack;
        mExecutedPoint = new ArrayList<LinePointBean>();
        gpsInfo = GpsInfoBean.getInstance();
        autoPlayService = new AutoPlayServiceImpl(context);
    }

    @Override
    public void run() {
        while (autoJudgeHandler.getTaskStateEnum() != TaskStateEnum.FORCT_CLOSE) {
            if (null == mLineInfo || null == mLineInfo.getLinePoints()) return;
            if (mExecutedPoint.size() > 0) { //释放点位
                LinePointBean linePoint = autoPlayService.releaseLinePoint(mExecutedPoint, gpsInfo);
                if (null != linePoint) mExecutedPoint.remove(linePoint);
            }
            for (LinePointBean linePoint : mLineInfo.getLinePoints()) {
                //判断车速是否小于最低车速,是则继续检测该点位
                if (gpsInfo.getSpeed() < 2) continue;
                if (autoPlayService.isRelease(mExecutedPoint, linePoint)) continue;
                //播报点位
                if (autoPlayService.isPlayLinePoint(gpsInfo, linePoint)) {
                    mExecutedPoint.add(linePoint);
                    //消息回掉
                    iExamStateCallBack.autoPlayProject(linePoint);
                }
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}